from selenium.webdriver import Edge
import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = Edge()

driver.get("https://max.book118.com/html/2019/0215/6101202153002010.shtm")
time.sleep(1)

# driver.find_element_by_xpath('//a[@id="full"]').click()
driver.find_element_by_id("full").click()
# print(driver.find_elements_by_class_name("webpreview-item"))
# print(driver.find_elements_by_tag_name("img"))

driver.switch_to_frame("layer_new_view_iframe")
# 通过srcoll滚动条来实现加载所有的图片
# driver.execute_script("document.documentElement.scrollLeft=10000") # 不好使
page = 0
while page <= 18:
    driver.execute_script("window.scrollTo(document.body.scrollHeight*{}, document.body.scrollHeight*{});".format(page/18, (page+1)/18))
    time.sleep(1)
    page += 1
    
imgs = driver.find_elements_by_xpath('//*[@id="newView"]/div[@class="bd"]/div[@class="webpreview-item"]/img')

for img in imgs:
    src = img.get_attribute("src")
    if src is None:
        # 直接通过图片加载事件来实现图片加载失败，可能需要更好的了解html、js等
        # img.onload()
        # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "img")))
        src = "https:{}".format(img.get_attribute("data-src"))
    print(src)

# driver.close()